package com.bike.controller.jump;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bike.controller.BaseController;
import com.bike.entity.HhBusiness;
import com.bike.entity.HhDining;
import com.bike.entity.HhOpenUser;
import com.bike.service.impl.HhBusinessServiceImpl;
import com.bike.service.impl.HhBusinessUserServiceImpl;
import com.bike.service.impl.HhDiningServiceImpl;
import com.bike.service.impl.HhOpenUserServiceImpl;
import com.bike.service.impl.HhPayConfigServiceImpl;
import com.bike.util.Constant;
import com.bike.util.PageData;
import com.bike.util.TimeManager;
import com.bike.util.UuidUtil;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.topit.show.aliapi.AliUtil;
import com.topit.show.wxapi.common.AuthorizeService;
import com.topit.show.wxapi.common.WxConstants;
import com.topit.show.wxapi.common.WxUtil;

 
@Controller
@RequestMapping("j")
public class HhJumpController extends BaseController {
	
		@Autowired
		private HhBusinessServiceImpl hhBusinessServiceImpl;
		@Autowired
		private HhDiningServiceImpl  hhDiningServiceImpl;
		@Autowired
		private HhOpenUserServiceImpl  openUserServiceImpl;
	
		/**
		 * 跳转到列表页面
		 */
		@RequestMapping("{diningId}")
		public String toList(@PathVariable Long diningId ,HttpServletRequest request,Model model){
			PageData pd=PageData.getToPageData(request);
			
			HhOpenUser hhOpenUser=(HhOpenUser) request.getSession().getAttribute("h5user");
			
			String userAgent = request.getHeader("User-Agent");
			
			String openId=null;
			 
			
			HhDining dining=hhDiningServiceImpl.selectByHhDiningId(diningId);
		
			if(hhOpenUser!=null){
				 
			}else{
				//微信
				if(userAgent.indexOf(Constant.wxAgent) > 0){
					//AuthorizeService authorizeService=new AuthorizeService();
					if(!pd.getString("code").equals("")){
						openId=WxUtil.getOpenId(pd);
					}
					if(StringUtils.isEmpty(openId)){
						return "redirect:"+WxUtil.getRedirectUrl("/hhe/j/"+diningId);
					}
				}else if(userAgent.indexOf(Constant.aliAgent) > 0){
					if(!pd.getString("auth_code").equals("")){
						openId=AliUtil.getAliOpenid(pd.getString("auth_code"), null);
					}
					if(StringUtils.isEmpty(openId)){
						return "redirect:"+AliUtil.getRedirectUrl("/hhe/j/"+diningId,dining.getBusinessId());
					}
				}else{
					openId="teste";
				}
				HhOpenUser user=openUserServiceImpl.selectByOpenId(openId);
				if(user==null){
					user=new HhOpenUser();
					user.setAddTime(TimeManager.currentTime());
					user.setOpenId(openId);
					openUserServiceImpl.saveOrUpdate(pd, user);
				}
				request.getSession().setAttribute(Constant.h5User, user);
			}
			
			HhBusiness business=hhBusinessServiceImpl.selectByHhBusinessId(dining.getBusinessId());
			model.addAttribute("dining", dining);
			model.addAttribute("business", business);
			model.addAttribute("pd", new PageData());
			return "/information/h5/index";
		}
	 
		
		
		
}
